package ro.licenta.main;

import ro.licenta.tama.Tama;
import ro.licenta.utilities.utils.PreferencesUtils;
import ro.licenta.utilities.utils.StaticUtils;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.RadioGroup;

import com.example.test.R;

public class SettingsActivity extends Activity {
	private Button startGame;
	private PreferencesUtils mPrefs;
	private Tama tTama;
	private CheckBox cbSound;
	private RadioGroup radiogDifficulty, radiogLifespan;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.settings);

		mPrefs = new PreferencesUtils(this);
		tTama = Tama.getInstance();

		startGame = (Button) findViewById(R.id.b_saveandstart);
		startGame.setOnClickListener(myClickListener);

		cbSound = (CheckBox) findViewById(R.id.cb_sound);

		radiogDifficulty = (RadioGroup) findViewById(R.id.rg_difficulty);
		radiogLifespan = (RadioGroup) findViewById(R.id.rg_lifespan);

	}

	@Override
	protected void onPause() {
		finish();
		System.out.println("OnPauseSettings");
		super.onPause();
	}

	@Override
	protected void onDestroy() {
		System.out.println("OnDestroySettings");
		super.onDestroy();
	}

	private OnClickListener myClickListener = new OnClickListener() {
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.b_saveandstart: {
				mPrefs.setGameStarted(true);
				tTama = Tama.getInstance();
				resetTama();
				// get sound;
				tTama.setSound(cbSound.isChecked());
				// get difficulty;
				
				switch (radiogDifficulty.getCheckedRadioButtonId()) {
				case R.id.r_dif_thesys: {
					tTama.setThesys(true);
					tTama.setUpdateService(StaticUtils.diffThesys);
					break;
				}
				case R.id.r_easy: {
					tTama.setUpdateService(StaticUtils.diffEasy);
					break;
				}
				case R.id.r_normal: {
					tTama.setUpdateService(StaticUtils.diffNormal);
					break;
				}
				case R.id.r_hard: {
					tTama.setUpdateService(StaticUtils.diffHard);
					break;
				}
				}
				
				switch (radiogLifespan.getCheckedRadioButtonId()) {
				case R.id.r_lifespan_thesys: {
					tTama.setThesys(true);
					tTama.setLifeSpan(StaticUtils.lifespanThesys);
					break;
				}
				case R.id.r_short: {
					tTama.setLifeSpan(StaticUtils.lifespanShort);
					break;
				}
				case R.id.r_medium: {
					tTama.setLifeSpan(StaticUtils.lifespanMedium);
					break;
				}
				case R.id.r_long: {
					tTama.setLifeSpan(StaticUtils.lifespanLong);
					break;
				}
				}
				System.out
						.println("---------------------------------------------------");
				System.out.println("Thesys is: " + tTama.isThesys());
				System.out.println("Sound is: " + cbSound.isChecked());
				System.out.println("LifeCycle is: " + tTama.getLifeCycle());
				System.out.println("Lifespan is: " + tTama.getLifespan());
				System.out.println("UpdateService is: "
						+ tTama.getUpdateService());
				System.out
						.println("---------------------------------------------------");
				Intent intent = new Intent(SettingsActivity.this,
						MainActivity.class);
				startActivity(intent);
				finish();
				break;
			}
			}

		}
	};

	private void resetTama() {
		tTama = Tama.getInstance();
		mPrefs.ResetCache();
		if (mPrefs.getHp(StaticUtils.mprefsError) == StaticUtils.mprefsError) {
			mPrefs.setHp(StaticUtils.startStat);
		}
		if (mPrefs.getEne(StaticUtils.mprefsError) == StaticUtils.mprefsError) {
			mPrefs.setEne(StaticUtils.startStat);
		}
		if (mPrefs.getFull(StaticUtils.mprefsError) == StaticUtils.mprefsError) {
			mPrefs.setFull(StaticUtils.startStat);
		}
		if (mPrefs.getFun(StaticUtils.mprefsError) == StaticUtils.mprefsError) {
			mPrefs.setFun(StaticUtils.startStat);
		}
		if (mPrefs.getAge(StaticUtils.mprefsError) == StaticUtils.mprefsError) {
			mPrefs.setAge(StaticUtils.ageChild);
		}

		if (mPrefs.getSleeping() == false) {
			mPrefs.setSleeping(false);
		}

		if (mPrefs.getGameStarted() == false) {
			mPrefs.setGameStarted(true);
		}
		tTama.setThesys(false);
		tTama.setHp(StaticUtils.startStat);
		tTama.setEne(StaticUtils.startStat);
		tTama.setFull(StaticUtils.startStat);
		tTama.setFun(StaticUtils.startStat);
		tTama.setAge(StaticUtils.ageChild);
		tTama.setSleeping(false);
		tTama.setToyPosition(StaticUtils.error);
		tTama.setBedPosition(StaticUtils.error);
		tTama.setStuffPosition(StaticUtils.error);
		tTama.setLampPosition(StaticUtils.error);
		tTama.setFoodPosition(StaticUtils.error);
		tTama.setUpdateService(StaticUtils.error);
		tTama.setLifeSpan(StaticUtils.error);
		tTama.setFed(false);
		tTama.setPlay(false);

	}

}
